#usr/bin/python

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class canvas_painter(QGraphicsScene):
	
	def __init__(self,  points, flags ="chrom" ,  parent=None):
		QGraphicsScene.__init__(self,  parent)
		#self.setMaximumSize(40,  600)
		self.painter =QPainter(self)
		self.points =points
		self.flags =flags
		#origin points
		self.xy_origin = QPointF(40.,  600.)
		self.x_max = QPointF(1040,  600.)
		self.y_max =QPointF(40., 60.)
		self.background =QRectF(self.xy_origin,  QPointF(1040.,  60))
		
		#Mode zoom, non zoom, pan
		self.Mode ="normal"
		
		self.cursor_in =False
		#remember position when mouse is pressed
		self.lastpos =QPointF()
		self.newpos =QPointF()
		self.rect =QRectF()
		
	def set_mode_to_zoom(self):
		print "triggered"
		self.Mode ="zoom"
		self.setMouseTracking(False)
		self.repaint()
		
	def data_normalization(self):pass
	
	def set_background(self):
		self.painter.begin(self)
		self.painter.setPen(QPen(Qt.white ))
		self.painter.drawRect(self.background)
		self.painter.fillRect(self.background, Qt.white)
		self.painter.end()
	
	
	def make_grid_lines(self):
		#vertical lines
		xmin =self.xy_origin.x()
		ymin =self.xy_origin.y()
		xmax =self.x_max.x()
		ymax =self.y_max.y()
		i =xmin
		j = ymin
		
		self.painter.begin(self)
		self.painter.setPen(QPen(Qt.black,  1,  Qt.DashLine ) )
		while i < xmax:
			self.painter.drawLine(QLineF(QPointF(i, ymin) ,  QPointF(i,  ymax)))
			i += 50.
		while j > ymax:
			self.painter.drawLine(QLineF(QPointF(xmin, j) ,  QPointF(xmax, j)))
			j -= 50.
		self.painter.end()
	
	def drawData(self):
		pass
	
	def drawLegends(self):
		self.painter.begin(self)
		self.painter.setPen(QPen(Qt.black,  0.5,  Qt.SolidLine ) )
		if self.flags =="chrom":
			self.painter.drawText ( QPointF((self.xy_origin.x()+self.x_max.x())/2.,  self.xy_origin.y()+30),  "Retention Time")
			self.painter.drawText ( QPointF(  self.xy_origin.x()-30,  (self.xy_origin.y()+self.y_max.y())/2.),  "Intensity")
		self.painter.end()
		
	def drawAxes(self):
		self.painter.begin(self)
		self.painter.setPen(QPen(Qt.black,  2,  Qt.SolidLine ) )
		self.painter.drawLine(QLineF(self.xy_origin,  self.x_max))
		self.painter.drawLine(QLineF(self.xy_origin, self.y_max))
		self.painter.end()
	
	
	
	####EVENTS HANDLING
	def paintEvent(self, event):
		print "event called"
		self.set_background()
		self.drawAxes()
		self.make_grid_lines()
		self.drawLegends()
		#if self.cursor_in:
		#	self.drawTicks()
		if self.Mode =="zoom":
			print "zoom event appele"
			self.lastpos.setY(self.xy_origin.y())
			self.newpos.setY(self.y_max.y())
			self.painter.begin(self)
			self.painter.setPen( QPen( Qt.black, 1, Qt.SolidLine ) )
			self.rect = QRectF(self.lastpos, self.newpos)
			self.painter.drawRect(self.rect)
			self.painter.setOpacity(.5)
			self.painter.fillRect(self.rect, Qt.blue)
			self.painter.end()
			#self.Mode ="normal"
			#self.repaint()
		if self.Mode =="normal" and self.cursor_in:
			print "homma"
			self.painter.begin(self)
			self.painter.setPen( QPen( Qt.blue, 1, Qt.SolidLine ) )
			p1 =QPointF(self.cursor().pos().x(),  self.xy_origin.y())
			p2 =QPointF(self.cursor().pos().x(),  self.y_max.y())
			self.painter.drawLine(QLineF(p1, p2))
			#self.painter.drawLine(QLineF(self.cursor().posF(), str(self.cursor().pos().x())))
			self.painter.end()
		
			
	def drawTicks(self):
		self.painter.begin(self)
		self.painter.setPen( QPen( Qt.black, 1, Qt.SolidLine ) )
		self.painter.drawText(self.cursor().pos(), str(self.cursor().pos().x()))
		self.painter.end()
		
	def enterEvent(self, e):
		self.cursor_in = True
		if self.Mode != "zoom":
			self.setMouseTracking(True)
			
		
	def leaveEvent(self, e):
		self.cursor_in =False
		if self.Mode != "zoom":
			self.setMouseTracking(False)
		
	def mousePressEvent(self, e):
		if self.Mode =="zoom":
			self.lastpos =e.posF()
			
	def mouseReleaseEvent(self, e):
		print "mouse released", e.pos()
		
	def mouseMoveEvent(self, e):
		if self.Mode =="zoom":
			self.newpos =e.posF()
			self.repaint()
		elif self.Mode =="normal" and self.cursor_in:
			QToolTip.showText(self.cursor().pos(), "x:"+str(self.cursor().pos().x())+", y:"+str(self.cursor().pos().y()))
			self.repaint()
		else:
			e.ignore()
		print e.pos().x()


class Toolbar (QToolBar):
	def __init__(self):
		QToolBar.__init__(self)
		self.actions_ =[]
		self.actions_.append(self.addAction("Zoom"))
		self.actions_.append(self.addAction("Pan"))
		self.actions_.append(self.addAction("flip"))

class Figure(QWidget):
	def __init__(self,  toolbar,  canvas,  parent =None):
		QWidget.__init__(self,  parent)
		self.tb =toolbar
		self.figure = canvas
		self.setupUi()
		
		
	def setupUi(self):
		self.vb =QVBoxLayout(self)
		self.vb.addWidget(self.figure)
		self.vb.addWidget(self.tb)
		
		
class canvas_controller:
	def __init__(self,  canvas):
		self.canvas = canvas
		QObject.connect( self.canvas.tb.actions_[0], SIGNAL("triggered()"), self.canvas.figure.set_mode_to_zoom)

		
		
if __name__=="__main__":
	import sys
	app = QApplication(sys.argv)
	
	points =[]
	for i in range (10):
		points.append(QPointF(float(i),float(i+100)  ))
	canvas = canvas_painter(points)
	tb =Toolbar()
	c =Figure(tb, canvas)
	cntrl = canvas_controller(c)
	c.show()
	app.exec_()
